<template>
  <div ref="history" class="batch-history"></div>
</template>
<script>
import dayjs from "dayjs";
export default {
  name: "BatchHistory",
  props: {
    endTime: {
      default: ""
    },
    startTime: {
      default: ""
    },
    type: {
      default: ""
    }
  },

  data() {
    return {
      myChart: null,
      xData: [],
      yData: []
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    drawLine() {
      this.myChart = this.$echarts.init(this.$refs.history);
      var option = {
        title: {
          text: "运行任务数"
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985"
            }
          }
        },
        legend: {
          bottom: "2%"
        },
        toolbox: {
          feature: {
            saveAsImage: {
              show: true,
              title: "下载"
            }
          }
        },
        grid: {
          left: "5%",
          right: "8%",
          bottom: "10%",
          containLabel: true
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: this.xData
            // axisLabel :{
            //     interval:0
            // },
          }
        ],
        yAxis: [
          {
            type: "value"
          }
        ],
        series: [
          {
            name: "数量",
            type: "line",
            stack: "总量",
            areaStyle: {},

            data: this.yData
          }
        ]
      };
      this.myChart.setOption(option);
      window.addEventListener("resize", () => {
        this.myChart.resize();
      });
    },
    initData() {
      this.$http.batch
        .getMetrics({
          start_time: this.startTime,
          end_time: this.endTime,
          type: this.type,
          project_id: this.$route.query.project_id
        })
        .then(res => {
          var data = res;
          this.xData = data.map(item => {
            return dayjs(item.date).format("YYYY-MM-DD HH:mm:ss");
          });
          this.yData = data.map(item => {
            return item.count;
          });
          this.drawLine();
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.batch-history {
  width: 100%;
  height: 100%;
}
</style>
